'use strict';

const Service = require('egg').Service;
const nodemailer = require('nodemailer');
const utility = require('utility')

let emailS = '1172824156@qq.com';

const transporter = nodemailer.createTransport({
  service: 'qq',
  secureConnection: true,
  port: 465,
  auth: {
    user: emailS, // 账号
    pass: 'zhbomdifqxwdgibc', // SMTP授权码
  },
});

class MailService extends Service {
  async sendMail(email, subject, html) {
    const mailOptions = {
      from: emailS, // 发送者,与上面的user一致
      to: email,   // 接收者,可以同时发送多个,以逗号隔开
      subject,   // 标题
      html,
    };
    try {
      await transporter.sendMail(mailOptions);
      return true;
    } catch (err) {
      return false;
    }
  }

  async verifyStr() {
    let r = Math.random() * 100;
    let n = await utility.sha1(String(r));
    return n;
  }
}

module.exports = MailService;